<?php
# Sample script for uploading all files in a given directory using RASS.
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_once '../RawsClient/Raws/Rass.php';

# Provide the path to a local testfile, that can be uploaded via RASS
define('LOCAL_DIR', '/path/to/your/local/upload/dir');
# subdir on the CDN to which files need to be uploaded
define('CDN_DIR', '/my_uploaded_files/');

# Provide your own RASS credentials here
define('USER', 'xxx'); # your user account name
define('PWD', 'xxx'); # your user account pwd
define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")

define('BASE', 'http://rass.' . CDN . '.rambla.be/');

# all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);
  
  # get file paths into array
  $a_files = array();
  $handle = opendir(LOCAL_DIR);
  while (false !== ($file = readdir($handle))) {
    // if file isn't this directory or its parent, add it to the results
    if ($file != "." && $file != "..") {
      $a_files[] = LOCAL_DIR . "/" . $file;
    }
  }
  closedir($handle);
  
  # upload files to your CDN dir
  foreach($a_files as $path) {
    try {
      # Upload the file to the CDN, using the original filename
      $item_entry = $rass->postItem(CDN_DIR, basename($path), $path);
      # Retrieve the relative path to the file on the CDN
      echo "Created file with path: " . $item_entry->path . "\n";
      # Retrieve the filename on the CDN
      echo "Filename: " . $item_entry->content->params->name->text . "\n";
      # Size of the uploaded file in bytes
      echo "Filesize: " . $item_entry->content->params->size->text . "\n";
      # Get the CDN download URL from the entry's 'enclosure' link
      echo "Public download location of the uploaded file: " . $item_entry->get_enclosure_url() . "\n";
      # Note: the URL for accessing this new item resource is available in the "Location" header of the response
      echo "New item resource can be accessed at the URL: " . $rass->getResponse()->getHeader("Location") . "\n";
    }
    catch(Zend_Gdata_App_Exception $e) # IF UPLOAD FAILS, exception is raised
    {
        # Report the exception to the user
        echo "\nCaught exception: " . get_class($e) . "\n";
        echo "Message: " . $e->getMessage() . "\n";
        # get the HTTP status code
        echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
        echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";

        # Do something here, if you want to retry the upload ...
    }
  }
  
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
